﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Public.Ocean.Petrel.Extensions.Internal;

namespace Public.Ocean.Petrel.Extensions.UnitTest
{
    [TestClass]
    public class EnumerationsTest
    {
        [TestMethod]
        public void FlattenOne()
        {
            string item = "Item";
            var result = new List<string>(Enumerations.Flatten(new[] { item }, i => new string[0]));
            Assert.AreEqual(1, result.Count);
            Assert.AreEqual(item, result[0]);
        }

        [TestMethod]
        public void FlattenInfinite()
        {
            string item = "Item";
            var enumerator = Enumerations.Flatten(new[] { item }, i => new[] { i }).GetEnumerator();
            for (int i = 0; i < 100; i++)
            {
                Assert.IsTrue(enumerator.MoveNext());
                Assert.AreEqual(item, enumerator.Current);
            }
        }

        [TestMethod]
        public void FlattenTree()
        {
            var tree = new Dictionary<string, List<string>>();
            tree.Add("A", new List<string>() { "AA", "AB", "AC" });
            tree.Add("AA", new List<string>() { "AAA", "AAB" });
            tree.Add("B", new List<string>() { "BA", "BB" });

            var result9 = new List<string>(Enumerations.Flatten(new[] { "A", "B" }, n => tree.ContainsKey(n) ? tree[n] : new List<string>()));
            Assert.AreEqual(9, result9.Count);

            tree["AA"] = null;
            var result7 = new List<string>(Enumerations.Flatten(new[] { "A", "B" }, n => tree.ContainsKey(n) ? tree[n] : new List<string>()));
            Assert.AreEqual(7, result7.Count);

            tree.Remove("A");
            tree.Remove("AA");
            tree["B"].Add(null);
            var result3 = new List<string>(Enumerations.Flatten(new[] { "B" }, n => tree.ContainsKey(n) ? tree[n] : new List<string>()));
            Assert.AreEqual(3, result3.Count);
        }

        [TestMethod]
        public void FlattenList()
        {
            var list = new List<string>() { "A", "B" };
            var result2 = new List<string>(Enumerations.Flatten(list, null));
            Assert.AreEqual(2, result2.Count);
        }

        [TestMethod]
        public void FlattenObjectList()
        {
            var input = new System.Collections.ArrayList();
            input.Add("A");
            var children = new System.Collections.ArrayList();
            children.Add("AA");
            children.Add("AB");

            var result = new List<string>(Enumerations.Flatten(input, i => i.Equals("A") ? children : null).Cast<string>());

            Assert.AreEqual(3, result.Count);
        }
    }
}
